package controllers

import (
	"dm_gin_1/config"
	"dm_gin_1/models"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type UserController struct {
	UserRepo *models.UserRepository
}

func NewUserController() *UserController {
	db := config.InitDB()
	repo := &models.UserRepository{DB: db}
	return &UserController{UserRepo: repo}
}

func (uc *UserController) GetUsers(c *gin.Context) {
	users, err := uc.UserRepo.GetAll()
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}
	c.JSON(http.StatusOK, users)
}

func (uc *UserController) GetUserById(c *gin.Context) {
	id := c.Param("id")
	uintId, _ := strconv.ParseUint(id, 10, 32)
	user, err := uc.UserRepo.GetById(uint(uintId))
	if err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
		return
	}
	c.JSON(http.StatusOK, user)
}
